package com.NetWork.lesson2;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

//客户端
public class TcpClient {
    public static void main(String[] args) {

        OutputStream os = null;
        Socket socket = null;

        try {
            //1.要知道服务器的地址
            InetAddress serverIp = InetAddress.getByName("127.0.0.1");
            //2.端口号
            int port = 9999;
            //3.创建一个Socket连接
            socket = new Socket(serverIp,port);
            //4.发送消息 IO流
            os = socket.getOutputStream();
            os.write("你好，服务器，客户端想你发来问候".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}

    